package cn.endeavour.basic.controller;

import cn.endeavour.basic.exception.BusinessException;
import cn.endeavour.basic.util.AjaxResult;
import cn.endeavour.user.dto.LoginDto;
import cn.endeavour.user.dto.UserDto;
import cn.endeavour.user.service.ILoginInfoService;
import cn.endeavour.user.service.IUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 登录接口
 */
@RestController
@RequestMapping("/login")
@Api(tags = "登录接口",description = "主要用作登录")
public class LoginController {

    @Autowired
    private ILoginInfoService loginInfoService;

    @Autowired
    private IUserService userService;

    /**
     * 退出登录
     * @param request
     */
    @PostMapping("/logout")
    public void logout(HttpServletRequest request) {
        userService.logout(request);
    }

    /**
     * 通过手机验证码进行登录
     * @param userDto 登录信息
     * @return 处理结果
     */
    @PostMapping("/phone")
    public AjaxResult loginByPhone(@RequestBody UserDto userDto) {
        try {
            return loginInfoService.loginByPhone(userDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误" + e.getMessage());
        }
    }

    /**
     * 微信绑定注册
     * @param params 参数
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult binderWechat(@RequestBody Map<String,String> params){
        try {
            return  loginInfoService.binderWechat(params);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统错误！"+e.getMessage());
        }
    }
    /**
     * 通过用户名以及密码登录
     * @param loginDto 登录信息
     * @return 处理结果
     */
    @PostMapping("/account")
    public AjaxResult loginByAccount(@RequestBody LoginDto loginDto, HttpServletRequest request) {
        try {
            return loginInfoService.loginByAccount(loginDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误" + e.getMessage());
        }
    }

    /**
     * 微信登录
     * @param params 前端
     * @return 处理结果
     */
    @PostMapping("/wechat")
    public AjaxResult loginByWeChat(@RequestBody Map<String, String> params) {
        try {
            return loginInfoService.loginWechat(params);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误" + e.getMessage());
        }
    }


}
